/*
 * @Author: wangtao
 * @Date: 2020-08-20 14:07:42
 * @LastEditors: 汪滔
 * @LastEditTime: 2021-06-03 11:12:42
 * @Description: 签署合同反馈页面
 */

import React from 'react';
import {
  View,
  StyleSheet,
  Image,
  Text,
  BackHandler,
  TouchableOpacity,
} from 'react-native';
import {
  iconBigSuccessGreen,
} from '@/images';
import { msg, Button } from '@/common';
import {
  mainBgColorLightGray,
  px2dp,
  fontColorBlack,
  fontColorCoffee,
  fontColorSecDeepGray,
  isAndroid,
  mainBgColorWhite,
  splitLineColorLightGray,
} from '@/styles';

const { Submit } = Button;

export default class ContractFeedBack extends React.Component {
  static navigationOptions = ({ navigation }) => ({
    headerLeft: (
      <TouchableOpacity
        activeOpacity={0.8}
        style={isAndroid ? { marginLeft: 14 } : {}}
        onPress={() => { }}
      />
    ),
    headerTitle: '申请结果',
  });

  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    if (isAndroid) {
      this.listener = BackHandler.addEventListener(
        'hardwareBackPress',
        this.onBack,
      );
    }
  }

  componentWillUnmount() {
    this.listener && this.listener.remove();
  }

  render() {
    const { navigation } = this.props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { data } = params;
    return (
      <View style={styles.container}>
        <Image source={iconBigSuccessGreen} resizeMode='stretch' style={styles.img} />
        <Text style={styles.title}>签署成功</Text>
        <Text style={styles.desc}>合同已完成在小马区块链上的安全存证</Text>
        <Text style={styles.subDesc}>存证哈希值</Text>
        <Text style={[styles.subDesc, styles.subDescText]}>
          {data && data.hashCode}
        </Text>
        <Submit
          text='查看合同'
          onClick={() => {
            msg.emit('router: goToNext', {
              routeName: 'ContractDetail',
              routerFrom: 'contractFeedback',
              contractStatus: 4,
              data
            });
          }}
          boxStyle={styles.boxStyle}
          btnTextStyle={{
            fontSize: px2dp(28),
          }}
        />
        <Submit
          text='返回'
          onClick={this.goWhere}
          boxStyle={[styles.boxStyle, { backgroundColor: mainBgColorLightGray }]}
          btnTextStyle={{
            fontSize: px2dp(28),
            color: fontColorCoffee,
          }}
        />
      </View>
    );
  }

  onBack = () => {
    this.goWhere();
    return true;
  };

  // 根据业务跳转不同充值路由
  goWhere = () => {
    const { navigation } = this.props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { routerFrom, data } = params;
    if (routerFrom == 'AddTruck') {
      msg.emit('router: refreshRoute', {
        routeName: 'CarOwnerTruckList',
        routerFrom: ''
      });
      msg.emit('router: goToNext', {
        routeName: 'CarOwnerTruckList', // 点击返回跳转到那里去
        // type: 'view',
        // data,
        routerFrom: '' // 从哪里发起的
      });
    } else if (routerFrom == 'ContractList') {
      msg.emit('router: refreshRoute', {
        routeName: 'ContractSYSList',
        routerFrom
      });
      msg.emit('router: goToNext', {
        routeName: 'ContractSYSList', // 点击返回跳转到那里去
        type: 'view',
        data,
        routerFrom // 从哪里发起的
      });
    } else if (routerFrom == 'DriverMain') {
      msg.emit('router: refreshRoute', {
        routeName: 'DriverTab',
      });
      msg.emit('router: goToNext', {
        routeName: 'DriverMain', // 点击返回跳转到那里去
      });
    }
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: mainBgColorWhite,
  },
  img: {
    width: px2dp(294),
    height: px2dp(294),
    marginTop: px2dp(35),
  },
  title: {
    fontSize: px2dp(40),
    color: fontColorBlack,
    fontWeight: 'bold',
  },
  desc: {
    fontSize: px2dp(28),
    marginTop: px2dp(12),
    color: fontColorBlack,
  },
  subDesc: {
    fontSize: px2dp(28),
    marginTop: px2dp(12),
    color: fontColorSecDeepGray,
    width: px2dp(460),
    textAlign: 'center',
  },
  subDescText: {
    textDecorationLine: 'underline',
    textDecorationColor: splitLineColorLightGray,
  },
  boxStyle: {
    width: px2dp(520),
    height: px2dp(80),
    borderWidth: 1,
    borderColor: fontColorCoffee,
    borderRadius: px2dp(8),
    marginTop: px2dp(64),
    position: 'absolute',
    bottom: px2dp(120),

  },
});
